package com.lightkits.wms.domain.purchasereceive;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.CommonMethodsUtil;
import com.lightkits.common.db.entity.FndOrganizationBase;
import com.lightkits.common.db.entity.FndOrganizationTl;
import com.lightkits.common.model.CurrentWorkerInfo;
import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.database.dao.WmsScmPoHeaderMapper;
import com.lightkits.wms.database.model.SimPurHeadModel;
import com.lightkits.wms.dto.purchasereceive.GetPurchaseSimRequestType;
import com.lightkits.wms.dto.purchasereceive.GetPurchaseSimResponseType;
import com.lightkits.wms.dto.purchasereceive.models.SimPurchaseDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 采购订单列表接口
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/25 16:14
 */
@Component
public class SimPurchaseExecutor implements ServiceExecutor<GetPurchaseSimRequestType, GetPurchaseSimResponseType> {
    private final WmsScmPoHeaderMapper scmPoHeaderMapper;

    @Autowired
    public SimPurchaseExecutor(WmsScmPoHeaderMapper scmPoHeaderMapper) {
        this.scmPoHeaderMapper = scmPoHeaderMapper;
    }

    @Override
    public GetPurchaseSimResponseType service(GetPurchaseSimRequestType request) {
        GetPurchaseSimResponseType response = new GetPurchaseSimResponseType();
        response.setSuccessful(true);
        try {
            List<SimPurchaseDTO> dtos = new ArrayList<>();
            CurrentWorkerInfo currentWorkerInfo = CommonMethodsUtil.getCurrentWorkerInfo(null);

            FndOrganizationBase base = new FndOrganizationBase().selectOne(
                    new EntityWrapper()
                            .eq(FndOrganizationTl.ORGANIZATION_ID, currentWorkerInfo.getDefaultOrganizationId()));
            if (ObjectUtil.isEmpty(base)) {
                response.setSuccessful(false);
                response.setMessage("get orgCode failed");
                return response;
            }
            List<SimPurHeadModel> models = scmPoHeaderMapper.GetSimPurchaseHeadByNum(request.getPoNumOrSupplierName(),
                    base.getOrganizationId().toString());
            models.forEach(model -> {
                SimPurchaseDTO dto = new SimPurchaseDTO();
                dto.setPoId(model.getPurchaseId());
                dto.setPoNum(model.getPurchaseNum());
                dto.setPoStatus(model.getPurchaseStatus());
                dto.setSupplierName(model.getSupplierName());
                dtos.add(dto);
            });
            response.setSimPurchaseDTOS(dtos);
        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);
        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("get Simple Purchase Head Error");
        }
        return response;

    }
}
